package com.jf.cloud.platform.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.delivery.feign.AreaFeignClient;
import com.jf.cloud.api.delivery.vo.AreaVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.platform.dto.InfoCateDTO;
import com.jf.cloud.platform.mapper.InfoCateMapper;
import com.jf.cloud.platform.model.InfoCate;
import com.jf.cloud.platform.service.InfoCateService;
import com.jf.cloud.platform.vo.InfoCateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zz
 * @date 2023-05-31 13:58:00
 */
@Service
public class InfoCateServiceImpl implements InfoCateService {

    @Autowired
    private InfoCateMapper infoCateMapper;

    @Autowired
    private AreaFeignClient areaFeignClient;

    @Override
    public PageVO<InfoCateVO> page(PageDTO pageDTO, InfoCateDTO infoCateDTO) {

        if(null !=infoCateDTO.getAreaName()){
          AreaVO areaVO= areaFeignClient.getAreaByName(infoCateDTO.getAreaName()).getData();
          if(null!=areaVO){
              infoCateDTO.setAreaCode(areaVO.getAreaId());
          }
        }
        PageVO<InfoCateVO> list = PageUtil.doPage(pageDTO, () -> infoCateMapper.list(infoCateDTO));
        list.getList().stream().forEach(e -> e.setAreaName(areaFeignClient.getArea(e.getAreaCode()).getData().getAreaName()));
        return list;
    }

    @Override
    public InfoCate getById(Long id) {
        return infoCateMapper.getById(id);
    }

    @Override
    public void save(InfoCate infoCate) {
        infoCateMapper.save(infoCate);
    }

    @Override
    public void update(InfoCate infoCate) {
        infoCateMapper.update(infoCate);
    }

    @Override
    public void deleteById(Long id) {
        infoCateMapper.deleteById(id);
    }
}
